package com.pengliu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.xerces.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;
import com.pengliu.common.bean.OcrManage;
import com.pengliu.common.bean.OcrUser;
import com.pengliu.common.exception.ServiceException;
import com.pengliu.config.BaseController;
import com.pengliu.dll.Ocr_dll;
import com.pengliu.service.OcrManageService;
import com.pengliu.util.AjaxResult;
import com.pengliu.util.Lists;

@Controller
@RequestMapping("/ocr")
public class OcrController extends BaseController{
	
	@Value("${host}")
	private String host;
	
	@Autowired
	private OcrManageService ocrManageService;
	
	@RequestMapping("/1")
	public ModelAndView s1() {
		ModelAndView modelAndView = this.getModelAndView("/ocr/1");
		return modelAndView;
	}
	
	@ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public AjaxResult test(@RequestParam("file") MultipartFile multipartFile ) throws Exception {
		String result = "";
		if (!multipartFile.isEmpty()) {
			byte[] bytes = multipartFile.getBytes();
			result = Ocr_dll.INSTANCE.ocr(bytes, bytes.length);
		}
		return AjaxResult.getSuccessrResult(result);
	}
	
	@ResponseBody
	@CrossOrigin
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public AjaxResult ocr(
    		String imgBase64,
    		String userName,
    		String passWord
    ) throws Exception {
		if(StringUtils.isBlank(imgBase64)) {
			throw new ServiceException("[imgBase64]验证码图片不能为空");
		}
		if(StringUtils.isBlank(userName)) {
			throw new ServiceException("[userName]用户名不能为空");
		}
		if(StringUtils.isBlank(passWord)) {
			throw new ServiceException("[passWord]密码不能为空");
		}
		
		OcrUser ocrUser = ocrManageService.checkUser(userName, passWord);
		if(null == ocrUser) {
			throw new ServiceException("账号或密码错误!");
		}
		
		
		String base64 = "base64,";
		String head = "";
		if(imgBase64.contains(base64)) {
			head = imgBase64.substring(0, imgBase64.indexOf(base64));
			imgBase64 = imgBase64.substring(imgBase64.indexOf(base64)+7);
		}
		
		String result = "";
		byte[] bytes = Base64.decode(imgBase64);
		result = Ocr_dll.INSTANCE.ocr(bytes, bytes.length);
		
		if(result.equals("")) {
			throw new ServiceException("未识别出结果!");
		}
		
		imgBase64 = head+base64+imgBase64;
		
		OcrManage ocrManage = new OcrManage();
		ocrManage.setImg(imgBase64);
		ocrManage.setOcrUserId(ocrUser.getId());
		ocrManage.setResult(result);
		ocrManageService.insertOcrManage(ocrManage);
		
		return AjaxResult.getSuccessrResult(result);
	}
	
	@RequestMapping("/user-list")
	public ModelAndView userList() {
    	//获取当前登陆用户
    	Integer currentSysUserId = getCurrentSysUser().getId();
    	
    	List<OcrUser> listOcrUser = ocrManageService.getOcrUsers(currentSysUserId);
    	
		ModelAndView modelAndView = this.getModelAndView("/ocr/user-list");
		modelAndView.addObject("listMap", listOcrUser);
		return modelAndView;
	}
	
	@RequestMapping("/user-add")
	public ModelAndView userAdd() {
		ModelAndView modelAndView = this.getModelAndView("/ocr/user-add");
		return modelAndView;
	}
	
	@RequestMapping("/ocr-test")
	public ModelAndView ocrTest() {
		ModelAndView modelAndView = this.getModelAndView("/ocr/ocr-test");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/user_add")
	public AjaxResult user_add(String userName,String passWord) throws ServiceException {
		//获取当前登陆用户
		Integer currentSysUserId = getCurrentSysUser().getId();
		ocrManageService.addOcrUser(userName, passWord,currentSysUserId);
		return AjaxResult.getSuccessrResult(true);
	}
	
	@ResponseBody
	@RequestMapping("/user_stop")
	public AjaxResult user_stop(Integer id) throws ServiceException {
		ocrManageService.stopOcrUser(id);
		return AjaxResult.getSuccessrResult(true);
	}
	
	@ResponseBody
	@RequestMapping("/user_start")
	public AjaxResult user_start(Integer id) throws ServiceException {
		ocrManageService.startOcrUser(id);
		return AjaxResult.getSuccessrResult(true);
	}
	
	@ResponseBody
	@RequestMapping("/user_del")
	public AjaxResult user_del(Integer id) throws ServiceException {
		ocrManageService.delOcrUser(id);
		return AjaxResult.getSuccessrResult(true);
	}
	
	@ResponseBody
	@RequestMapping("/file_del")
	public AjaxResult file_del(Long id) throws ServiceException {
		ocrManageService.file_del(id);
		return AjaxResult.getSuccessrResult(true);
	}
	
	@RequestMapping("/ocr-views")
	public ModelAndView ocrViews(
			Integer ocrUserId,
			String startTimeStr,
			String endTimeStr,
			Integer pageNum,
			Integer pageSize
	) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("ocrUserId", ocrUserId);
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		params.put("startTimeStr", startTimeStr);
		params.put("endTimeStr", endTimeStr);
		
		Integer currentSysUserId = getCurrentSysUser().getId();
		List<OcrUser> allOcrUser = ocrManageService.getOcrUsers(currentSysUserId);
		
		List<Integer> ocrUserIds = Lists.newArrayList();
		if(null == ocrUserId) {
			for (OcrUser ocrUser : allOcrUser) {
				ocrUserIds.add(ocrUser.getId());
			}
    	}else {
    		ocrUserIds.add(ocrUserId);
    	}
    	
    	PageInfo<OcrManage> pageInfo = ocrManageService.getOcrManages(ocrUserIds, pageNum, 10);
    	
		ModelAndView modelAndView = this.getModelAndView("/ocr/ocr-views");
		modelAndView.addObject("pageInfo", pageInfo);
		modelAndView.addObject("allOcrUser", allOcrUser);
		modelAndView.addObject("params", params);
		return modelAndView;
	}
	
}
